////
////  ASBridge.swift
////  xmall
////
////  Created by zhuxietong on 2020/2/10.
////  Copyright © 2020 xmall. All rights reserved.
////
//
//import Foundation
//import soapp
//import AsyncDisplayKit
//import Eelay
//
//
//extension TP{
//    open class Auto{
//
//
//        init() {
//
//        }
//        public class Width: Auto {
//
//        }
//        public class Height: Auto {
//
//        }
//    }
//}
//
//
//
//
//open class ASBridge<AutoType:TP.Auto>: JoView {
//
//    typealias auto = AutoType
//    public enum Fixed {
//        case width(CGFloat)
//        case height(CGFloat)
//    }
//    var _loaded = false
//
//    public var nodeSize:CGSize = CGSize(width: 100000, height: 10){
//        didSet{
//            if (auto.self is TP.Auto.Width.Type){
//                print("+sss+++IIII--|\(nodeSize)")
//                _value.constant = nodeSize.width
//            }
//            if (auto.self is TP.Auto.Height.Type){
//                print("++s++IIII--|\(nodeSize)")
//                _value.constant = nodeSize.height
//
//            }
//            superview?.layoutIfNeeded()
//            updateSize(nodeSize)
//            setNeedsLayout()
//            layoutIfNeeded()
////            superview?.setNeedsLayout()
////            superview?.layoutIfNeeded()
//        }
//    }
//
//    public var updateSize:(_:CGSize)-> Void = {(size) in}
//
//    public override func addLayoutRules() {
//        self.eelay = [
//            [e.view,[ee.X.Y]]
//        ]
//        _ = _value
//        print("++++OOOIIII\(_value)")
//    }
//
//
//    public var changeFrame:(_:CGRect)->Void = {_ in}
//
//
//
//    open override var bounds: CGRect{
//         set{
//            super.bounds = newValue
//            changeFrame(self.frame)
//         }get{
//             return super.bounds
//         }
//     }
//
//
//    public var node = ASDisplayNode(){
//        didSet{
//            self.e.e = self.node
//        }
//    }
//
//
//    public func loadNode() {
//        setNeedsLayout()
//        layoutIfNeeded()
//    }
//
//    var _e:Display? = nil
//    public var e:Display{
//        get{
//            guard let a = _e else {
//                let e = Display(e: node)
//                print("++++++9((((")
//                weak var ws = self
//                e.getSize = {
//                    (size) in
//                    ws?.nodeSize = size
//                }
//                self._e = e
//                return e
//            }
//            return a
//        }
//    }
//
//
//    public class Display:MoDisplayNode {
//        var getSize:(_ size:CGSize) -> Void = {(e) in};
//        var e:ASDisplayNode{
//            didSet{
//                setNeedsLayout()
//                layoutIfNeeded()
//            }
//        }
//        init(e:ASDisplayNode) {
//            self.e = e
//            super.init()
//            addLayoutRules()
//        }
//        public override func addLayoutRules() {
//
//
//        }
//        public required init() {
//            fatalError("init() has not been implemented")
//        }
//        open override func specGetter() -> _Spec {
//
//            if (auto.self is TP.Auto.Width.Type){
//                return .stack(["h",_grow(0),_shrink(0),&-.stretch], childs: [e.Spec])
//            }else{
//                return .stack(["v",_grow(0),_shrink(0),&-.stretch], childs: [e.Spec])
//            }
//
//        }
//        open override func layoutDidFinish() {
//            print("+++++OOOOO----))1")
//            self.getSize(e.bounds.size)
//        }
//    }
//
//
//    open override func mo_model_reload() {
//        if let n = node as? MoDisplayNode{
//            n.mo.model = mo.model
//        }
//    }
//
//
//
//    lazy var _value:NSLayoutConstraint = {
//        if (auto.self is TP.Auto.Height.Type){
//            let _height = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: self.nodeSize.height)
//                  _height.priority =  UILayoutPriority(rawValue: 900)
//                self.addConstraint(_height)
//            return _height
//        }else{
//            let _width = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: self.nodeSize.width)
//                  _width.priority =  UILayoutPriority(rawValue: 900)
//                self.addConstraint(_width)
//            return _width
//        }
//
//    }()
//
//
//
//
//
//
//    open override func didMoveToSuperview() {
//        if (!_loaded){
//
//
//            loadNode()
//            _loaded = true
//        }
//    }
//
//
//}
//
//
//  ASBridge.swift
//  xmall
//
//  Created by zhuxietong on 2020/2/10.
//  Copyright © 2020 xmall. All rights reserved.
//

import Foundation
import AsyncDisplayKit
import Eelay


open class ViewNode<ViewType:UIView>:ASDisplayNode{
    public typealias View = ViewType
    public override init() {
        
        super.init()
        self.setViewBlock {
            return View()
        }
    }
    open var content: View{
        get{
            return super.view as? View ?? View()

        }
    }
    
}


open class ASBridge: JoView {
    var _loaded = false
    public var nodeSize:CGSize = CGSize(width: 0, height: 10){
        didSet{
            _height.constant = nodeSize.height
        
            superview?.layoutIfNeeded()
            updateSize(nodeSize)
            setNeedsLayout()
            layoutIfNeeded()
            superview?.setNeedsLayout()
            superview?.layoutIfNeeded()
        }
    }
    
    public var updateSize:(_:CGSize)-> Void = {(size) in}
    
    public override func addLayoutRules() {
    }

    
    public var changeFrame:(_:CGRect)->Void = {_ in}
    

    
    open override var bounds: CGRect{
         set{
            super.bounds = newValue
            changeFrame(self.frame)
         }get{
             return super.bounds
         }
     }
 
    
    public var node = ASDisplayNode(){
        didSet{
            self.e.e = self.node
        }
    }
    
    
    public func loadNode() {
        setNeedsLayout()
        layoutIfNeeded()
    }
    
    var _e:Display? = nil
    public var e:Display{
        get{
            guard let a = _e else {
                let e = Display(e: node)
                weak var ws = self
                e.getSize = {
                    (size) in
                    ws?.nodeSize = size
                }
                self._e = e
                return e
            }
            return a
        }
    }
       
    
    public class Display:MoDisplayNode {
        var getSize:(_ size:CGSize) -> Void = {(e) in};
        var e:ASDisplayNode{
            didSet{
                setNeedsLayout()
                layoutIfNeeded()
            }
        }
        init(e:ASDisplayNode) {
            self.e = e
            super.init()
            addLayoutRules()
        }
        public override func addLayoutRules() {
            
            
        }
        public required init() {
            fatalError("init() has not been implemented")
        }
        open override func specGetter() -> _Spec {
            return .stack(["v",_grow(0),_shrink(0),&-.stretch], childs: [e.Spec])
        }
        open override func layoutDidFinish() {
            self.getSize(e.bounds.size)
        }
    }
    
    
    open override func mo_model_reload() {
        if let n = node as? MoDisplayNode{
            n.mo.model = mo.model
        }
    }
    
    
    

    var _height = NSLayoutConstraint()
    open override func didMoveToSuperview() {
        if (!_loaded){
            self.eelay = [
                [e.view,[ee.T.L.B.R]]
            ]
            _height = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: self.nodeSize.height)
            _height.priority =  UILayoutPriority(rawValue: 1000)
            self.superview?.addConstraint(_height)
            loadNode()
            _loaded = true
        }
    }
    

}

open class UINode<T:ASDisplayNode>:UIView{
    let node = T()
    public init() {
        super.init(frame: .zero)
    }
    
    var contentSize:CGSize? = nil
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    open func addLayoutRules() {
        
    }
    
}
